package com.wyh.service.serviceImpl.groupOne.customer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.po.groupOne.customer.MasterDataContacts;
import com.wyh.domain.vo.groupOne.customer.MasterDataCustomerVO;
import com.wyh.mapper.groupOne.customer.MasterDataContactsMapper;
import com.wyh.mapper.groupOne.customer.MasterDataCustomerMapper;
import com.wyh.service.groupOne.customer.MasterDataCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MasterDataMasterDataCustomerServiceImpl implements MasterDataCustomerService {

    @Autowired
    private MasterDataCustomerMapper masterDataCustomerMapper;

    @Autowired
    private MasterDataContactsMapper masterDataContactsMapper;

    @Override
    public List<MasterDataCustomerVO> selectAll(MasterDataCustomerVO customer) {
        return masterDataCustomerMapper.selectAll(customer);
    }

    @Override
    public MasterDataCustomerVO selectAccurately(String name, String telephone, Integer type, Integer lnvolvinglitigation) {
        if (name == null && telephone == null || (Objects.requireNonNull(name).isEmpty() && telephone.isEmpty())) {
            throw new CustomException("4001", "请输入客户名称和手机号");
        } else if (telephone == null || telephone.isEmpty()) {
            throw new CustomException("4001", "请输入客户手机号");
        } else if (name == null || name.isEmpty()) {
            throw new CustomException("4001", "请输入客户名称");
        } else {
            MasterDataCustomerVO dbcustomer = masterDataCustomerMapper.selectAccurately(name, telephone, type, lnvolvinglitigation);
            if (ObjectUtil.isNotEmpty(dbcustomer)) { //当查询到的客户不为空时才能查到联系人
                List<MasterDataContacts> masterDataContactsList = masterDataContactsMapper.selectAll(dbcustomer.getId());
                if (!masterDataContactsList.isEmpty()) {
                    dbcustomer.setMasterDataContactsList(masterDataContactsList);
                }

            }
            return dbcustomer;
        }
    }

    @Override
    public int insert(MasterDataCustomerVO customer) {

        MasterDataCustomerVO dbcustomer = masterDataCustomerMapper.selectByName(customer.getName());
        MasterDataCustomerVO dbcreditcode = masterDataCustomerMapper.selectByCreditcode(customer.getCreditcode());
        if (customer.getName().isEmpty() || customer.getTelephone().isEmpty() || customer.getCreditcode().isEmpty() || customer.getType() == null) {
            throw new CustomException("4001", "用户名、手机号、社会信用码、客户类型不能为空");
        }

        if (ObjectUtil.isNotEmpty(dbcustomer)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        // 校验客户名称长度
        if (customer.getName().length() < 4 || customer.getName().length() > 50) {
            throw new CustomException("400", "客户名称长度应为4至50字符");
        }


        // 判断统一社会信用码在集团公司内是否唯一
        if (ObjectUtil.isNotEmpty(dbcreditcode)) {
            throw new CustomException("400", "统一社会信用码重复");
        }

        return masterDataCustomerMapper.insert(customer);

    }

    @Override
    public MasterDataCustomerVO selectDetailBasic(Integer id) {
        if (id == null) {
            throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return masterDataCustomerMapper.selectDetailBasic(id);
    }

    @Override
    public int update(MasterDataCustomerVO customer) {
        MasterDataCustomerVO dbcustomer = masterDataCustomerMapper.selectDetailBasic(customer.getId());
        if (!ObjectUtil.isEmpty(dbcustomer)) {
            DateTime date = DateUtil.date();
            dbcustomer.setLastupdatetime(date.toString());
            dbcustomer.setUserid(customer.getUserid());

            if (dbcustomer.getState() == 1) {
                return masterDataCustomerMapper.update(customer);
            }
            throw new CustomException(ResultCodeEnum.STATE_ERROR);
        }
        return 0;

    }

    @Override
    public int updateState(MasterDataCustomerVO masterDataCustomerVO) {
        Integer state = masterDataCustomerVO.getState();
        MasterDataCustomerVO dbcustomer = masterDataCustomerMapper.selectDetailBasic(masterDataCustomerVO.getId());
        if (!ObjectUtil.isEmpty(dbcustomer)) {
            if (state == 0 || state == 1) {
                DateTime date = DateUtil.date();
                dbcustomer.setLastupdatetime(date.toString());
                dbcustomer.setUserid(masterDataCustomerVO.getUserid());
                masterDataCustomerMapper.update(dbcustomer);
                return masterDataCustomerMapper.updateState(masterDataCustomerVO);
            }
        }

        throw new CustomException(ResultCodeEnum.STATE_ERROR);
    }
}
